package com.lottery.shadowbd.ui.activity;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.lottery.shadowbd.R;
import com.lottery.shadowbd.support.Constant;

public class WebActivity extends BaseActivity {

    private WebView wv_lottery;
    private ProgressBar pBar_web;
    private ImageView iv_lot_back;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_web);

        iv_lot_back = (ImageView) findViewById(R.id.iv_lot_back);

        wv_lottery = (WebView) findViewById(R.id.wv_lottery);

        pBar_web = (ProgressBar) findViewById(R.id.pBar_web);
    }

    @Override
    protected void initData() {
        mContext = this;

        if(getIntent() != null) {
            loadWeb(getIntent().getStringExtra(Constant.KeyWebUrl));
        }
    }

    @Override
    protected void initListener() {
        iv_lot_back.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.iv_lot_back://返回
                finish();
                break;
            default:
                break;
        }
    }

    //加载网页
    private void loadWeb(String webUrl) {
        if (webUrl == null) {
            return;
        }

        wv_lottery.getSettings().setJavaScriptEnabled(true);
        wv_lottery.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); //取消滚动条白边效果
        wv_lottery.getSettings().setDefaultTextEncodingName("UTF-8");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            wv_lottery.getSettings().setMixedContentMode(wv_lottery.getSettings().MIXED_CONTENT_ALWAYS_ALLOW);  //注意安卓5.0以上的权限
        }

        wv_lottery.loadUrl(webUrl);

        /*wv_lottery.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                onDownloadClick(url);
            }
        });*/

        wv_lottery.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //拦截webviewurl跳转
                pBar_web.setVisibility(View.VISIBLE);
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });

        wv_lottery.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    pBar_web.setVisibility(View.GONE);
                } else {
                    pBar_web.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public boolean onJsAlert(WebView view, String url, String message,
                                     JsResult result) {
                // TODO Auto-generated method stub
                return super.onJsAlert(view, url, message, result);
            }
        });
    }
}
